﻿using System;
using System.Collections;
using UnityEngine;

namespace Assets
{
    public class InputManager : MonoBehaviour
    {
        bool clicking;
        [SerializeField] TerrainGenerator generator;
        // Use this for initialization
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                clicking = true;
            }
            else if (Input.GetMouseButton(0) && clicking)
            {
                Clicking();
            }
            else if (Input.GetMouseButtonUp(0))
            {
                clicking = false;
            }
        }

        private void Clicking()
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (new Plane(new Vector3(0, 0, 1f), new Vector3(0, 0, 0)).Raycast(ray, out float enter))
            {
                var point = ray.GetPoint(enter);
                generator.Touching(point);
            }
        }
    }
}